#!/bin/sh

###
### comment : 导出hive表的数据进入kudu中
### args1 日期
### args2 任务配置文件
### author : eric

if [ -z $1 ];then
	uname1=$(uname)
	if [ 'Linux' = $uname1 ];then
      day=$(date -d "1 days ago" +%Y%m%d)
    else
	  day=$(date -v -1d +%Y%m%d)
    fi
else
  day=$1
fi

## 缓存
param_day=$day

## 本年第一天
firstDayOfYear=$(date -d"$day" +'%Y0101')
echo "firstDayOfYear：${firstDayOfYear}"
## 本月第一天
firstDayOfMonth=$(date -d"$day" +'%Y%m01')
echo "firstDayOfMonth：${firstDayOfMonth}"
## 本周第一天
whichday=$(date -d $day +%w)
firstDayOfWeek=$(date -d "$day -$[${whichday}-1] days" +%Y%m%d)
echo "firstDayOfWeek：${firstDayOfWeek}"





config_file=$2
CUR_DIR=$(cd "$(dirname "$0")"; pwd)

if [ "${config_file}" != "" ];then
    config_file=${CUR_DIR}/${config_file}
else
    config_file=${CUR_DIR}/config/*.config	
fi

source ${CUR_DIR}/config/config.properties

for task_config in `ls ${config_file}`
do
    #是否是周表
    result=$(echo $task_config | grep "pwi")
    if [[ "$result" != "" ]];then
       day=$firstDayOfWeek
    fi
    #是否是月表
    result=$(echo $task_config | grep "pmi")
    if [[ "$result" != "" ]];then
       day=$firstDayOfMonth
    fi
     #是否是年表
    result=$(echo $task_config | grep "pyi")
    if [[ "$result" != "" ]];then
       day=$firstDayOfYear
    fi

    temp_sql_file=${task_config}.sql
    source ${task_config}
    echo ${sql} > ${temp_sql_file}
    cat ${temp_sql_file}

    ${PRESTO_HOME}/bin/presto --server ${PRESTO_HOST}:${PRESTO_PORT}  -f ${temp_sql_file}

    rm -rf ${temp_sql_file}
    sleep 10
    ### 重新赋值
    day=$param_day
done


echo "hive_to_kudu.sh执行耗时$((`date +%s`-START_TIME))秒..."
